package com.asiainfo.controller;

import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.asiainfo.bean.UserBean;
import com.asiainfo.bean.UserSessionBean;
import com.asiainfo.dao.BaseDao;


import com.asiainfo.util.SessionInfoUtil;





@Controller
@RequestMapping("/loginController")
public class LoginController extends ModelAndView{
	
	private Logger logger = LogManager.getLogger(this.getClass());
	
	

	/**
	 * 消息操作类型(delete:删除操作;update:更新操作)
	 */
	private String type;

	/**
	 * 数据操作封装DAO
	 */
	@Autowired
	private BaseDao dao;

	/**
	 * 方法描述：跳转到登录页面
	 * 
	 * return 登录页面Model
	 */
	@RequestMapping("/login.do")
	public ModelAndView index() {
		this.setViewName("login");
		// 进入一个页面是将该页面需要到的常量放入JSONObject中
		// 调用toJSONString方法，会自动转换为标准的json格式
		// sysConstant是约定好的变量名称，不能更改
		// JSONObject jo = new JSONObject();
		 //jo.put("param1", "1");
		// jo.put("param2", "1'2/?!-%--^\"");
		// jo.put("param3", "13");
		 //this.addObject("sysConstant",jo.toJSONString());
	
		return this;	
		}

	
	/**
	 * 方法描述：跳转到模拟手机登录页面
	 * 
	 * return 模拟手机登录页面Model
	 */
	@RequestMapping("/loginPhone.do")
	public ModelAndView indexPhone() {
		this.setViewName("helper/loginPhone");
		// 进入一个页面是将该页面需要到的常量放入JSONObject中
		// 调用toJSONString方法，会自动转换为标准的json格式
		// sysConstant是约定好的变量名称，不能更改
		// JSONObject jo = new JSONObject();
		 //jo.put("param1", "1");
		// jo.put("param2", "1'2/?!-%--^\"");
		// jo.put("param3", "13");
		 //this.addObject("sysConstant",jo.toJSONString());
	
		return this;	
		}

	
	
	
	/**
	 * 方法描述：验证用户的登录操作
	 * 
	 */
	@RequestMapping(value = "/validateUserLogin.do")
	 
	public @ResponseBody
	JSONObject validateUserLogin(@RequestBody UserBean userBean, 
			HttpServletRequest request) throws Exception {
		JSONObject retMsg = new JSONObject();
		HttpSession session = request.getSession();
		float balance = 0;
		String userPhone = userBean.getPhone().toUpperCase();
		
		UserBean userInfo = dao.getDAO().selectOne(
				"LoginMapper.queryUserInfoByID", userPhone);
		
		UserSessionBean userSessBean = new UserSessionBean();
		if(userInfo!=null){
		userSessBean.setPhone(userInfo.getPhone());
		userSessBean.setUserName(userInfo.getUserName());
		userSessBean.setUserGender(userInfo.getUserGender());
		userSessBean.setIdNumber(userInfo.getIdNumber());
		userSessBean.setOperatorId(userInfo.getOperatorId());
		userSessBean.setOperatorName(userInfo.getOperatorName());
		userSessBean.setCityName(userInfo.getCityName());
		userSessBean.setProvName(userInfo.getProvName());
		userSessBean.setUserAddress(userInfo.getUserAddress());
		userSessBean.setReal_time_balance(userInfo.getReal_time_balance());
		
		SessionInfoUtil.setUserSessBean(session, userSessBean);
		
		if(userInfo.getReal_time_balance()!=null){
			balance =Float.parseFloat(userInfo.getReal_time_balance());
			}
		}
		
		
		
		
		int isPhoneFlag = (Integer)this.dao.getDAO().selectOne("LoginMapper.queryLoginPhone", userBean);
		retMsg.put("isPhoneFlag", isPhoneFlag);
		
		int isLoginFlag = (Integer)this.dao.getDAO().selectOne("LoginMapper.queryLoginPassword", userBean);
		retMsg.put("isLoginFlag", isLoginFlag);
		
		if(isPhoneFlag==0){
			retMsg.put("flag", false);
		}else {
			if(isLoginFlag==0){
			retMsg.put("flag", false);
		    }else if(isLoginFlag==1){
		    	if(balance<10){
					retMsg.put("NoMoney", true);
				}else{
		    	  retMsg.put("flag", true);
				}
		      }
		}
		return retMsg;
	}
	
	/**
	 * 方法描述：用户登出系统
	 * 
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/logout.do")
	public @ResponseBody
	boolean userLogout(HttpSession session) throws Exception {
		SessionInfoUtil.removeUserSessBean(session);
		return true;
	}
	
}
